package io.zhiller.ollamaclient.ui.component

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Close
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import io.zhiller.ollamaclient.utils.ScreenUtils


@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun BottomDrawer(
  modifier: Modifier = Modifier,
  title: String = "标题",
  height: Dp? = null, // 可自定义高度
  onDismissRequest: () -> Unit,
  content: @Composable () -> Unit,
) {
  val sheetState = rememberModalBottomSheetState(
    skipPartiallyExpanded = true
  )
  
  val heightDp = ScreenUtils.getScreenHeightDp(LocalContext.current).times(.75f)
  
  ModalBottomSheet(
    onDismissRequest = onDismissRequest,
    sheetState = sheetState,
    shape = RoundedCornerShape(topStart = 16.dp, topEnd = 16.dp),
    modifier = modifier,
  ) {
    Column(
      modifier = Modifier
        .fillMaxWidth()
        .clip(RoundedCornerShape(12.dp))
        .then(
          if (height != null) {
            Modifier.height(height)
          } else {
            Modifier.heightIn(min = 100.dp, max = heightDp)
          }
        )
        .background(Color.White)
        .padding(16.dp)
    ) {
      Row(
        modifier = Modifier.fillMaxWidth(),
        horizontalArrangement = Arrangement.SpaceBetween,
        verticalAlignment = Alignment.CenterVertically
      ) {
        Text(
          text = title,
          style = MaterialTheme.typography.titleMedium,
          fontWeight = FontWeight.Bold
        )
        IconButton(onClick = onDismissRequest) {
          Icon(
            imageVector = Icons.Default.Close,
            contentDescription = "关闭"
          )
        }
      }
      Spacer(modifier = Modifier.height(12.dp))
      content()
    }
  }
}

@Composable
fun BottomDrawerInfo(
  modifier: Modifier = Modifier,
  title: String = "标题",
  content: String,
  height: Dp? = null, // 可自定义高度
  onDismissRequest: () -> Unit,
) {
  val scrollState = rememberScrollState()
  
  BottomDrawer(
    modifier = modifier,
    title = title,
    height = height,
    onDismissRequest = onDismissRequest,
  ) {
    Column(
      modifier = Modifier
        .fillMaxWidth(),
      verticalArrangement = Arrangement.spacedBy(8.dp)
    ) {
      Box(
        modifier = Modifier
          .verticalScroll(scrollState)
          .weight(1f),
      ) {
        Text(
          text = content,
          style = MaterialTheme.typography.bodyMedium,
        )
      }
      Button(
        modifier = Modifier.fillMaxWidth(),
        onClick = {
          onDismissRequest()
        }
      ) {
        Text(
          text = "知道了",
          style = MaterialTheme.typography.bodyMedium,
        )
      }
    }
  }
}

@Composable
fun BottomDrawerInfoButton(
  modifier: Modifier = Modifier,
  title: String = "标题",
  content: String,
  buttonTitle: String,
  height: Dp? = null, // 可自定义高度
  onDismissRequest: () -> Unit,
  onConfirm: () -> Unit,
) {
  val scrollState = rememberScrollState()
  
  BottomDrawer(
    modifier = modifier,
    title = title,
    height = height,
    onDismissRequest = onDismissRequest,
  ) {
    Column(
      modifier = Modifier
        .fillMaxWidth(),
      verticalArrangement = Arrangement.spacedBy(8.dp)
    ) {
      Box(
        modifier = Modifier
          .verticalScroll(scrollState)
      ) {
        Text(
          text = content,
          style = MaterialTheme.typography.bodyMedium,
        )
      }
      Button(
        modifier = Modifier.fillMaxWidth(),
        onClick = {
          onConfirm()
        }
      ) {
        Text(
          text = buttonTitle,
          style = MaterialTheme.typography.bodyMedium,
        )
      }
    }
  }
}

//@PreviewBothLightAndDark
//@Composable
//fun BottomDrawerTestPreview(modifier: Modifier = Modifier) {
//  ZBoxTheme {
//    Surface {
//      var isDrawerOpen by remember { mutableStateOf(false) }
//      FloatingActionButton(onClick = { isDrawerOpen = true }) {
//        Text("打开抽屉")
//      }
//      if (isDrawerOpen) {
//        BottomDrawerInfo(
//          title = "自定义标题",
//          height = ScreenUtils.getScreenHeightDp(LocalContext.current).div(3),
//          content = TextConst.TEXT_LONG_TEXT,
//          onDismissRequest = { isDrawerOpen = false },
//        )
//      }
//    }
//  }
//}